<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 求obj中所有对象的和
        var obj = {
            children:[1, 2, 3, 4],
            parent: {
                n1: 2,
                n2: 4,
                n3: 6,
            },
            name: 'zhangsan',
            married :false,
            gf: null,
        }
        var sum = 0;
        for (var key in obj) {
            if (Array.isArray(obj[key])) {
                for (var i = 0; i < obj[key].length; i++) {
                    sum += obj[key][i];
                } 
            }
            if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
                for (var k in obj[key]) {
                    sum += obj[key][k];
                }
            }
        }
        console.log(sum);
        
        // 对象转数组 1
        var obj = {
            name: 'zhangsan',
            age: 18,
            height: 180,
        };
        var expect = [
            ['name', 'zhangsan'],
            ['age', 18],
            ['height', 180],
        ]
        function obj2arr1 (obj) {
            var arr = [];
            for (var key in obj) {
                var tmp = [key,obj[key]];
                arr.push(tmp);
            }
            return arr;
        }
        console.log(obj2arr1(obj));

        // 对象转数组 2
        function obj2arr2 (obj) {
            var arr = [];
            for (var k in obj) {
                var tmp = {
                    k: k,
                    v: obj[k],
                };
                arr.push(tmp);
            }
            return arr;
        }
        console.log(obj2arr2(obj));

        // 数组转对象 1
        // 输入
        var arr = [
            {
                k: 'name',
                v: 'zhangsan',
            },
            {
                k: 'age',
                v: 18,
            },
            {
                k: 'height',
                v: 180,
            },
        ];
        // 输出
        // var expect = {
        //     name: 'zhangsan',
        //     age: 18,
        //     height: 180,
        // };
        function arr2obj (arr) {
            var result = {};
            for (var i = 0; i < arr.length; i++) {
                var key = arr[i].k;
                var value = arr[i].v;
                result[key] = value;
            }
            return result;
        }
        console.log(arr2obj(arr));
        
        
        // 数组转对象 2
        // 输入
        var obj = [
            ['name', 'zhangsan'],
            ['age', 18],
            ['height', 180],
        ];
        // 输出
        // var expect = {
        //     name: 'zhangsan',
        //     age: 18,
        //     height: 180,
        // };
        function arr2obj2 (arr) {
            var result = {};
            for (var i = 0; i < arr.length; i++) {
                var arrItem = arr[i];
                for (var j = 0; j < arrItem.length; j++) {
                    var key = arrItem[0];
                    var value = arrItem[1];
                    result[key] = value;
                }
            }
            return result;
        }
        console.log(arr2obj2(obj));   
    </script>
</body>
</html>